package com.xxl.exception;

import com.xxl.util.GlobalResult;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.SignatureException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.annotation.Resource;
import javax.validation.ConstraintViolationException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 全局异常处理
 * @author xxl
 * @date 2023/4/1
 */
@RestControllerAdvice
@Slf4j
public class GlobalException {
    @Resource
    GlobalResult result;
    /**
     * 没被指明的异常
     */
    @ExceptionHandler(Exception.class)
    public Map<String, Object> exception(Exception e) {
        return result.result(500, "未知错误:"+e );
    }
    /**
     * 解析token发现时间过期了
     */
    @ExceptionHandler(ExpiredJwtException.class)
    public Map<String, Object> exception(ExpiredJwtException e) {
        return result.result(500, "登录过期请重新登录");
    }
    /**
     * token的密钥不能解析这个token
     */
    @ExceptionHandler(SignatureException.class)
    public Map<String, Object> exception(SignatureException e) {
        return result.result(500,"登录失败，token未知错误",new Object[]{"exception",e});
    }
    /**
     * 一般是jsr验证错误
     */

    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public Map<String, Object> exception(HttpRequestMethodNotSupportedException e) {
        return result.result(500,"方法不支持，请检查参数是否正确",new String[]{"NotSupportMethod",e.getMethod()});
    }
    @ExceptionHandler(IOException.class)
    public Map<String, Object> exception(IOException e) {
        return result.result(500,"上传失败/下载失败，具体原因"+e);
    }
    @ExceptionHandler(UnsupportedEncodingException.class)
    public Map<String, Object> exception(UnsupportedEncodingException e) {
        return result.result(500,"下载失败："+e);
    }
    @ExceptionHandler(FileNotFoundException.class)
    public Map<String, Object> exception(FileNotFoundException e) {
        return result.result(500,"下载失败："+e);
    }
    /**
     * 校验方法参数异常(不包括java Bean)   ConstraintViolationException 此异常不能加入数组否则 会报错
     */
    @ExceptionHandler(ConstraintViolationException.class)
    public Map<String, Object> exception(ConstraintViolationException e) {
        return result.result(500,"参数异常："+e);
    }
    /**
     *  校验javaBean
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Map<String, Object> exception(MethodArgumentNotValidException e) {
        int i = 6 / 2;
        System.out.println(i);
        Map<String, Object> collect = e.getFieldErrors().stream().collect(Collectors.toMap(k -> k.getField(), v -> v.getDefaultMessage()));

        return result.resultMap(500,"javaBean参数异常",collect);
    }
}
